<?php
/**
 * 零起飞-(07FLY-CRM)
 * ==============================================
 * 版权所有 2015-2028   成都零起飞网络，并保留所有权利。
 * 网站地址: http://www.07fly.xyz
 * ----------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ==============================================
 * Commor: kfrs <goodkfrs@QQ.com> 574249366
 * Date: 2019-10-3
 */

namespace app\plat\logic;

use think\Db;

/**
 * 上传数据管理=》逻辑层
 */
class Upload extends PlatBase
{
    private $files_path = '';//上传文件路径

    public function __construct()
    {
        $this->files_path = PATH_FILE . 'plat_upload' . DS;
        !is_dir($this->files_path) && mkdir($this->files_path, 0755, true);
    }

    /**
     * 客户列表列上传
     * @param array $data
     * Author: lingqifei created by at 2020/6/4 0004
     */
    public function uploadFile($data = [])
    {
        if (!empty($data['uploadfilefather'])) {
            $this->files_path = $this->files_path . $data['uploadfilefather'] . DS;
        }
        if (!empty($data['uploadfilepath'])) {
            $this->files_path = $this->files_path . $data['uploadfilepath'] . DS;
        }
        !is_dir($this->files_path) && mkdir($this->files_path, 0755, true);
        $object_info = request()->file('file');
        $object = $object_info->move($this->files_path, '');//保留原文件名 savename=‘’设置为空
        $filename = $object->getpathName();//保存路径
        return [RESULT_SUCCESS, '上传成功'];
    }

    /**
     * 上传目录文件列表
     * @param array $data
     * Author: lingqifei created by at 2020/6/4 0004
     */
    public function getUploadFile($data = [])
    {
        $result['data'] = [];
        if (!empty($data['uploadfilefather'])) {
            $this->files_path = $this->files_path . $data['uploadfilefather'] . DS;
        }
        if (!empty($data['uploadfilepath'])) {
            $this->files_path = $this->files_path . $data['uploadfilepath'] . DS;
        }
        //获取里面的文件包名
        $fp = new \lqf\Dir();
        $dirlist = $fp->listFile($this->files_path);//查看目录列表文件，必须是以斜杠结束
        foreach ($dirlist as $key => &$row) {
            $row['size'] = format_bytes($row['size']);
            $row['uptime'] = date("Y/m/d H:i:s", $row['mtime']);
        }
        $result['data'] = $dirlist;
        return $result;
    }

    /**
     * 上传目录文件列表=删除文件
     * @param array $data
     * Author: lingqifei created by at 2020/6/4 0004
     */
    public function delUploadFile($data = [])
    {
        //按路径+文件名删除
        if (!empty($data['pathname'])) {
            if (file_exists($data['pathname'])) {
                unlink($data['pathname']);
            }
        }
        //上级文件路径
        if (!empty($data['uploadfilefather'])) {
            $this->files_path = $this->files_path . $data['uploadfilefather'] . DS;
        }
        //按文件名删除
        if (!empty($data['uploadfilepath']) && !empty($data['basename'])) {
            $data['pathname'] = $this->files_path . $data['uploadfilepath'] . DS . $data['basename'];
            if (file_exists($data['pathname'])) {
                unlink($data['pathname']);
            }
        }
        return [RESULT_SUCCESS, '删除成功'];
    }

    /**
     * 根据文件名获取文件路径
     * @param $data array()
     * array(
     * 'uploadfilefather'=>'upload/file/tpfile/',//上级文件路径
     * 'uploadfilepath'=>'upload/file/tpfile/',//文件目录
     * 'basename'=>'filename.txt',//文件名
     * )
     * @return string
     * @author: 开发人生 goodkfrs@qq.com
     * @Time: 2024/7/4 12:29
     */
    public function getUploadFilePathname($data = [])
    {
        $result = '';
        if (!empty($data['uploadfilefather'])) {
            $this->files_path = $this->files_path . $data['uploadfilefather'] . DS;
        }
        if (!empty($data['uploadfilepath']) && !empty($data['basename'])) {
            $data['pathname'] = $this->files_path . $data['uploadfilepath'] . DS . $data['basename'];
            if (file_exists($data['pathname'])) {
                $result = $data['pathname'];
            }
        }
        return $result;
    }
}

/**
 *在控制器调用的方式：
 *
 *
public function upload_item()
    //定义上传目录，必须存在
    $uploadfilepath = 'zx_order_tourist' . TEN_USER_ID;//上传文件目录

    //调用文件上传地址
    $uploadtarget = url('ZxOrder/upload_item', array('ajaxmodel' => 'upload', 'uploadfilepath' => $uploadfilepath));

    //模板文件
    $root_url = get_file_root_path();
    $tplfile = $root_url . 'upload/file/tpfile/zx_order_tourist.xlsx';

    //在回传时需要，默认自带数据，为上传的目录，
    $this->param['uploadfilepath'] = $uploadfilepath;

    if (!empty($this->param['ajaxmodel'])) {
        switch ($this->param['ajaxmodel']) {
            case 'get':
                return $this->logicUpload->getUploadFile($this->param);
                break;
            case 'del':
                return $this->jump($this->logicUpload->delUploadFile($this->param));
                break;
            case 'upload':
                return $this->jump($this->logicUpload->uploadFile($this->param));
                break;
            case 'import':
                return $this->jump($this->logicZxOrder->zxOrderGetUploadTourist($this->param));
                break;
        }
    }
    $this->assign('ajaxtarget', url());//上传接口地址
    $this->assign('uploadtarget', $uploadtarget);//文件上传接口地址
    $this->assign('tplfile', $tplfile);//模板文件路径的设置
    return $this->fetch('upload_item');//上传接口模板
}
 *
 **/
?>